package CCRMClient;

import java.io.IOException;

import javax.swing.JPanel;

import CCRMClient.GUI.EmployeePortal;
import CCRMClient.GUI.MainFrame;
import Common.ServerAnswer;
import Common.Entities.Employee;

/** This class starts the CCRM client.
* 
* It creates a client and connects to the server.
*  
* @author Ori Ziv
*
*/
public class ClientStart {
	public static CCRMClient client = null;
	public static Employee user;
	public static MainFrame appWindow;
	
	/**
	 * This method initiate the CCRMClient
	 * @param server - server host name or IP address
	 * @param port - server port (default 11170)
	 * @throws IOException
	 */
	public static void initCCRMClient (String server, String port) throws IOException {
		int serverPort;
		try {
			serverPort = Integer.parseInt(port);
		} catch (NumberFormatException e) {
			serverPort = 11170;
		}
		if (serverPort < 1) serverPort = 11170;
		
		client = new CCRMClient (server, serverPort);
	}
	
	/**
	 * This method switches panels in the client application window
	 * It removes the current panel from the window, adjust window size and add new panel
	 * @param panel - new panel to display
	 */
	public static void panelSwicher (JPanel panel) {
		appWindow.getContentPane().removeAll();
		int h = panel.getHeight();
		int w = panel.getWidth();
		appWindow.setSize(w,h);
		appWindow.getContentPane().add(panel);
		panel.setVisible(true);
		appWindow.repaint();
	}
	
	/**
	 * This method closes the CCRMClient connection and exit
	 */
	public static void quitApp () {
		if (client.isConnected())
			try {
				client.closeConnection();
			} catch (IOException e) {}
		appWindow.removeAll();
		appWindow.dispose();
		System.exit(0);
	}
	
	/**
	 * CCRMClient main
	 * @param args
	 */
	public static void main (String args[]) {
		appWindow = new MainFrame();
		appWindow.setVisible(true);
	}
}
